//
//  MaintenceDateCell.swift
//  koolcar
//
//  Created by xiantian on 2023/11/9.
//

import UIKit
import FSCalendar

enum MaintenceDIYDateCellType {
    case server_disable     //关闭 白底字灰
    case server_full        //已满 红
    case server_enable      //可预约 绿
}

class MaintenceDIYDateCell: FSCalendarCell {
    var cellType: MaintenceDIYDateCellType!
    override init!(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white;
        self.titleLabel.font = UIFont.init(name: Theme.PingFangSCMedium, size: 12)
        self.subtitleLabel.textColor = Theme.mainColor
        self.subtitleLabel.font = UIFont.init(name: Theme.PingFangSCRegular, size: 10)
        self.subtitleLabel.isHidden = true
        self.bringSubview(toFront: self.subtitleLabel)
        
    }
    
    required init!(coder aDecoder: NSCoder!) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.titleLabel.frame = self.bounds
        self.subtitleLabel.frame = CGRect.init(x: 0, y: frame.size.height - 14, width: frame.size.width, height: 14)
        
        let tempBounds = CGRect.init(x: 0, y: 0, width: self.bounds.size.width, height: self.bounds.size.height)
        self.shapeLayer.frame = tempBounds
        let path = UIBezierPath.init(roundedRect: tempBounds, cornerRadius: 0).cgPath
        if self.shapeLayer.path != path {
            self.shapeLayer.path = path
        }
        self.subtitleLabel.isHidden = true
        if self.dateIsToday {
            self.subtitleLabel.isHidden = false
            self.bringSubview(toFront: self.subtitleLabel)
        }
    }
    
    ///#71D883  选中
    func setDiyCellType(type: MaintenceDIYDateCellType, isToday: Bool) {
        self.cellType = type
        if isToday {
            self.subtitle = MMLocalizedString("today", comment: "今天")
            self.layoutSubviews()
        }
        self.setCellStyle()
    }
    
    
    ///重绘颜色
    override func configureAppearance() {
        super.configureAppearance()
        if self.isPlaceholder {
            self.eventIndicator.isHidden = true
            self.titleLabel.textColor = UIColor.lightGray
        }else{
            self.titleLabel.textColor = Theme.titleLabColor
            self.setCellStyle()
        }
        self.subtitleLabel.textColor = Theme.mainColor
    }
    
    private func setCellStyle(){
        switch self.cellType {
        case .server_full:
            //红
            self.isUserInteractionEnabled = false
            self.shapeLayer.backgroundColor = UIColor.init(hex: 0xE8181A, alpha: 0.15).cgColor
        case .server_enable:
            //可预约-绿色
            self.isUserInteractionEnabled = true
            self.shapeLayer.backgroundColor = UIColor.init(hex: 0x71D883, alpha: 0.3).cgColor
        default:
            //不可选
            self.titleLabel.textColor = UIColor.lightGray
            self.isUserInteractionEnabled = false
            self.shapeLayer.backgroundColor = UIColor.init(hex: 0xAFBED7, alpha: 0.3).cgColor
        }
    }
    
}
